package br.com.virtual.replicar.dao.impl;
  
import java.util.List;

import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.NoResultException;

import br.com.virtual.base.dao.impl.BaseDaoImpl;
import br.com.virtual.base.model.VirtualModel;
import br.com.virtual.replicar.dao.SincronizarDAO;
import br.com.virtual.replicar.model.AvaliacaoSinc;
import br.com.virtual.replicar.model.CategoriaSinc;
import br.com.virtual.replicar.model.EnderecoSinc;
import br.com.virtual.replicar.model.FotoSinc;
import br.com.virtual.replicar.model.GrupoSinc;
import br.com.virtual.replicar.model.InteresseSinc;
import br.com.virtual.replicar.model.ItemVendaSinc;
import br.com.virtual.replicar.model.PagamentoSinc;
import br.com.virtual.replicar.model.PessoaFisicaSinc;
import br.com.virtual.replicar.model.PessoaJuridicaSinc;
import br.com.virtual.replicar.model.PessoaSinc;
import br.com.virtual.replicar.model.ProdutoSinc;
import br.com.virtual.replicar.model.Sincronizar;
import br.com.virtual.replicar.model.UsuarioSinc;
import br.com.virtual.replicar.model.VendaSinc;


public class SincronizarDAOImpl extends BaseDaoImpl implements SincronizarDAO {


	public VirtualModel read( VirtualModel vm, Long id ){
		
		return getManager().find( vm.getClass(), id);
	}

	
	@Override
	public Sincronizar create(Sincronizar sincronizar) {

		getManager().persist(sincronizar);
		return sincronizar;
	}

	
	@Override
	public Sincronizar read(Long id) {

		return getManager().find(Sincronizar.class, id);
	}

	
	@Override
	public void update(Sincronizar sincronizar) {

		getManager().merge(sincronizar);
	}

	
	@Override
	public void delete(Sincronizar sincronizar) {
		
		getManager().remove(
					getManager().find(Sincronizar.class, sincronizar.getId())
					);
	}

	
	@Override
	public List<Sincronizar> getList() {

		List<Sincronizar> list = getManager().createQuery("select s from Sincronizar s order by s.id", Sincronizar.class).getResultList();
		return list;
	}


	
	@Override
	public Long count(){
		
		return getManager().createQuery("select count(s) from Sincronizar s", Long.class).getSingleResult();
	
	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public AvaliacaoSinc getVirtualReplicationModel(AvaliacaoSinc avaliacaoSinc) {
		
		AvaliacaoSinc a = getManager().find(AvaliacaoSinc.class, avaliacaoSinc.getId());
		getManager().detach(a);
		
		return a;
		
	}

	
	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public GrupoSinc getVirtualReplicationModel(GrupoSinc gruposinc) {
		
		GrupoSinc g = getManager().find(GrupoSinc.class, gruposinc.getId());
		getManager().detach(g);
		
		return g;
				
	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public CategoriaSinc getVirtualReplicationModel(CategoriaSinc categoriaSinc) {

		CategoriaSinc g = getManager().find(CategoriaSinc.class, categoriaSinc.getId());
		getManager().detach(g);
		
		return g;
	}


//	@Override
//	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
//	public ClienteSinc getVirtualReplicationModel(ClienteSinc clienteSinc) {
//		
//		ClienteSinc g = getManager().find(ClienteSinc.class, clienteSinc.getId());
//		getManager().detach(g);
//		
//		return g;
//	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public EnderecoSinc getVirtualReplicationModel(EnderecoSinc enderecoSinc) {
		
		EnderecoSinc g = getManager().find(EnderecoSinc.class, enderecoSinc.getId());
		getManager().detach(g);
		
		return g;
	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public FotoSinc getVirtualReplicationModel(FotoSinc fotoSinc) {
		
		FotoSinc g = getManager().find(FotoSinc.class, fotoSinc.getId());
		getManager().detach(g);
		
		return g;
	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public InteresseSinc getVirtualReplicationModel(InteresseSinc interesseSinc) {

		StringBuilder query = new StringBuilder();
		
		query.append("select distinct i from InteresseSinc i ");
		query.append("where i.id = :id");
		
		try{
			InteresseSinc g = getManager().createQuery( query.toString(), InteresseSinc.class).setParameter("id", interesseSinc.getId()).getSingleResult();
			getManager().detach(g);
			g.setCliente(null);
			
			return g;
			
		}catch(NoResultException nre){
			
			return null;
		}catch(Exception e){
			
			System.out.println("excessao");
			return null;
		}
	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public ItemVendaSinc getVirtualReplicationModel(ItemVendaSinc itemVendaSinc) {

		ItemVendaSinc g = getManager().find(ItemVendaSinc.class, itemVendaSinc.getId());
		getManager().detach(g);
		
		return g;
	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public PagamentoSinc getVirtualReplicationModel(PagamentoSinc pagamentoSinc) {

		PagamentoSinc g = getManager().find(PagamentoSinc.class, pagamentoSinc.getId());
		getManager().detach(g);
		
		return g;
	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public PessoaSinc getVirtualReplicationModel(PessoaSinc pagamentoSinc) {

		PessoaSinc g = getManager().find(PessoaSinc.class, pagamentoSinc.getId());
		getManager().detach(g);
		
		return g;
	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public PessoaFisicaSinc getVirtualReplicationModel(
			PessoaFisicaSinc pessoaFisicaSinc) {

		PessoaFisicaSinc g = getManager().find(PessoaFisicaSinc.class, pessoaFisicaSinc.getId());
		getManager().detach(g);
		
		return g;
	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public PessoaJuridicaSinc getVirtualReplicationModel(
			PessoaJuridicaSinc pessoaJuridicaSinc) {

		PessoaJuridicaSinc g = getManager().find(PessoaJuridicaSinc.class, pessoaJuridicaSinc.getId());
		getManager().detach(g);
		
		return g;
	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public ProdutoSinc getVirtualReplicationModel(ProdutoSinc produtoSinc) {

		ProdutoSinc g = getManager().find(ProdutoSinc.class, produtoSinc.getId());
		getManager().detach(g);
		
		return g;
	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public UsuarioSinc getVirtualReplicationModel(UsuarioSinc usuarioSinc) {

		UsuarioSinc g = getManager().find(UsuarioSinc.class, usuarioSinc.getId());
		getManager().detach(g);
		g.setPessoa(null);
		
		return g;
	}


	@Override
	@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
	public VendaSinc getVirtualReplicationModel(VendaSinc vendaSinc) {
	
		VendaSinc g = getManager().find(VendaSinc.class, vendaSinc.getId());
		
		for( ItemVendaSinc iv : g.getItens())
			getManager().detach(iv);
//		getManager().detach(g.getPagamento());
		getManager().detach(g);
		
		return g;
	}
	
}